package com.itcast.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.itcast.pojo.Collect;
import com.itcast.pojo.Goods;
import com.itcast.pojo.User;
import com.itcast.service.Collectservice;
import com.itcast.service.Goodsservice;
import com.itcast.util.OutUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@Controller
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private Collectservice collectservice;
    @Autowired
    private Goodsservice goodsservice;

    /**
     * 收藏添加
     * @param gid
     * @param response
     * @param session
     * @return
     */
    @RequestMapping(value ="/addLike")
    @ResponseBody
    public String addLike(Integer gid, Integer userId,HttpServletResponse response, HttpSession session) {
        String r ="";
        if (userId != null) {
            int uId = Integer.valueOf(userId);
            QueryWrapper<Collect> queryWrapper =new QueryWrapper<Collect>();
            queryWrapper.eq("goodId",gid);
            queryWrapper.eq("userId",uId);
            if (collectservice.getOne(queryWrapper)!=null){
                    r="ok";
            }else {
                Collect collect = new Collect();
                collect.setGoodId(gid);
                collect.setId(uId);
                collect.setCreateDate(new Date());
                boolean b= collectservice.save(collect);
                if (b) r="ok";
            }
        }else {
            r="err";
        }

        return r;
    }

    /**
     * 取消收藏
     * @param gid
     * @param userId
     * @param response
     * @param session
     * @return
     */
    @RequestMapping(value ="/delLike")
    @ResponseBody
    public String delLike(Integer gid, Integer userId,HttpServletResponse response, HttpSession session) {
        String r ="";
        if (userId != null) {
            int uId = Integer.valueOf(userId);
            QueryWrapper<Collect> queryWrapper = new QueryWrapper<Collect>();
            queryWrapper.eq("goodId", gid);
            queryWrapper.eq("userId", uId);
            System.out.println(gid);
            boolean falg = collectservice.remove(queryWrapper);
            if (falg) {
                r = "ok";
            } else {

            }
        }else {
            r = "err";
        }

        return r;
    }


    /**
     * 个人收藏显示
     */
    @RequestMapping("/getCollect")
    public ModelAndView showCollect(String userId, ModelAndView mv){
        List<Goods> goods = goodsservice.getByUserId(userId);
        mv.setViewName("/qd/collection.html");
        mv.addObject("goods",goods);
        System.out.println(goods.size());
        return  mv;
    }

    /**
     * 我的收藏
     * @param conditionData
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCollectByUser")
    public PageInfo<Goods> getCollectByUser(@RequestBody Map<String,String> conditionData){
        if (conditionData == null)
            return null;
            PageInfo<Goods> goodsByConditionAndPage =  goodsservice.getCollectByUser(conditionData);
            return goodsByConditionAndPage;
    }
}

